package com.estate.web.controller.report.home;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.estate.common.core.controller.BaseController;
import com.estate.common.core.domain.AjaxResult;
import com.estate.common.core.domain.entity.SysMenu;
import com.estate.common.core.domain.entity.SysUser;
import com.estate.common.core.domain.resp.SysMenuResp;
import com.estate.common.utils.StringUtils;
import com.estate.system.domain.vo.HomeSaleDataView;
import com.estate.system.domain.vo.ListingQueryBo;
import com.estate.system.mapper.SysUserMapper;
import com.estate.system.service.impl.DataEnquiriesServiceImpl;
import com.estate.system.service.impl.DataListingsServiceImpl;
import com.estate.system.service.impl.TransactionDataServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.estate.common.utils.PageUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/home/sale")
public class HomeSaleController extends BaseController {

    @Resource
    private DataEnquiriesServiceImpl dataEnquiriesServiceImpl;
    @Autowired
    private TransactionDataServiceImpl transactionDataServiceImpl;
    @Resource
    private DataListingsServiceImpl dataListingsServiceImpl;

    /**
     * 获取近七天新增客户咨询数量
     *
     * @param paramData
     * @return
     */
    @GetMapping("/getClientRequests")
    public AjaxResult getClientRequestsDetail(Map<String, Object> paramData) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> newEnquireMap = dataEnquiriesServiceImpl.getSevenDayNewEnquire(paramData);
        if (!CollectionUtils.isEmpty(newEnquireMap)) {
            Long sevenDaysAgoCount = (Long) newEnquireMap.get("sevenDaysAgoCount");
            Long fourteenDaysAgoCount = (Long) newEnquireMap.get("fourteenDaysAgoCount");
            Double upOrDown = (Double) newEnquireMap.get("addPercentage");
            result.put("clientRequest", sevenDaysAgoCount - fourteenDaysAgoCount);
            result.put("upOrDown", upOrDown);
        }
        return AjaxResult.success("success", result);
    }

    /**
     * 获取近七天新增房产数量
     *
     * @param paramData
     * @return
     */
    @GetMapping("/getNewListings")
    public AjaxResult getNewListings(Map<String, Object> paramData) {
        Map<String, Object> sevenDayNewListing = dataEnquiriesServiceImpl.getSevenDayNewListing(paramData);
        Map<String, Object> result = new HashMap<>();
        if (StringUtils.isNotEmpty(sevenDayNewListing)) {
            Long sevenDaysAgoCount = (Long) sevenDayNewListing.get("sevenDaysAgoCount");
            Long fourteenDaysAgoCount = (Long) sevenDayNewListing.get("fourteenDaysAgoCount");
            Double upOrDown = (Double) sevenDayNewListing.get("addPercentage");
            result.put("newListings", sevenDaysAgoCount - fourteenDaysAgoCount);
            result.put("upOrDown", upOrDown);
            return AjaxResult.success("success", result);
        } else {
            return AjaxResult.error("fail", null);
        }
    }

    @GetMapping("/getTransactions")
    public AjaxResult getTransactions(Map<String, Object> paramData) {
        Map<String, Object> transactionMap = transactionDataServiceImpl.getTransactions(paramData);
        Map<String, Object> result = new HashMap<>();

        if (StringUtils.isNotEmpty(transactionMap)) {
            Long sevenDaysAgoCount = (Long) transactionMap.get("sevenDaysAgoCount");
            Long fourteenDaysAgoCount = (Long) transactionMap.get("fourteenDaysAgoCount");
            Double upOrDown = (Double) transactionMap.get("addPercentage");
            result.put("newListings", sevenDaysAgoCount - fourteenDaysAgoCount);
            result.put("upOrDown", upOrDown);
            return AjaxResult.success("success", result);
        } else {
            return AjaxResult.error("fail", null);
        }
    }

    @GetMapping("/getRankingOfNewListing")
    public AjaxResult getRankingOfNewListing(Map<String, Object> paramData) {
        List<Map<String, Object>> listingCountByArea = dataListingsServiceImpl.getListingCountByArea(paramData).stream().sorted((o1, o2) -> {
            Long count1 = (Long) o1.get("value");
            Long count2 = (Long) o2.get("value");
            return count1 > count2 ? -1 : 1;

        }).limit(6).collect(Collectors.toList());
        return AjaxResult.success("success", listingCountByArea);
    }

    @GetMapping("/getRankingOfTransactionsNumber")
    public AjaxResult getRankingOfTransactionsNumber(Map<String, Object> paramData) {
        Map<Integer, Map<String, Object>> areaRankings = new HashMap<>();

        Map<String, Object> data1 = new HashMap<>();
        data1.put("area", "North Sydney"); // 郊区名称1
        data1.put("percentage", "1.6%");    // 交易占比1.6%
        areaRankings.put(1, data1);

        Map<String, Object> data2 = new HashMap<>();
        data2.put("area", "Burwood"); // 郊区名称2
        data2.put("percentage", "1.6%");    // 交易占比1.6%
        areaRankings.put(2, data2);

        Map<String, Object> data3 = new HashMap<>();
        data3.put("area", "Hurstville"); // 郊区名称3
        data3.put("percentage", "1.6%");    // 交易占比1.6%
        areaRankings.put(3, data3);

        Map<String, Object> data4 = new HashMap<>();
        data4.put("area", "Strathfield"); // 郊区名称4
        data4.put("percentage", "1.6%");    // 交易占比1.6%
        areaRankings.put(4, data4);

        Map<String, Object> data5 = new HashMap<>();
        data5.put("area", "Rhodes"); // 郊区名称5
        data5.put("percentage", "1.6%");    // 交易占比1.6%
        areaRankings.put(5, data5);

        Map<String, Object> data6 = new HashMap<>();
        data6.put("area", "Wentworth Point"); // 郊区名称6
        data6.put("percentage", "1.6%");    // 交易占比1.6%
        areaRankings.put(6, data6);
        return AjaxResult.success("success", areaRankings);
    }


    @GetMapping("/getTrendOfRequests")
    public AjaxResult getTrendOfRequests(Map<String, Object> paramData) {
        List<Map<String, Object>> result = dataEnquiriesServiceImpl.getEnquireByEnquireType(paramData);
        return AjaxResult.success("success", result);
    }

    /**
     * 新增租赁交易数量时间趋势变化
     * @param paramData
     * @return
     */
    @GetMapping("/getTrendOfTransactions")
    public AjaxResult getTrendOfTransactions(Map<String, Object> paramData) {
        Map<Integer, Integer> transactions = new HashMap<>();
        // 根据图表数据添加交易趋势点
        transactions.put(1100, 15);   // 1k: 25%
        transactions.put(2900, 20);   // 2k: 30%
        transactions.put(3800, 35);   // 3k: 35%
        transactions.put(4700, 44);   // 4k: 40%
        transactions.put(5600, 46);   // 5k: 45%
        transactions.put(6500, 51);   // 6k: 50%
        transactions.put(7400, 55);   // 7k: 55%
        transactions.put(8200, 62);   // 8k: 60%
        transactions.put(9100, 63);   // 9k: 65%
        transactions.put(10200, 70);  // 10k: 70% (峰值)
        transactions.put(11400, 45);  // 11k: 65%
        transactions.put(12200, 65);  // 12k: 65%
        return AjaxResult.success("success", transactions);
    }


    @GetMapping("/getDataView")
    public AjaxResult getDataView(HomeSaleDataView bo) {
        startPage();
        Page<HomeSaleDataView> dataViewPageList = dataListingsServiceImpl.getDataView(bo);
        return AjaxResult.success("查询成功", getDataTable(dataViewPageList));
    }

}
